<template>
	<el-container>
		<el-aside width="200px" v-loading="showGrouploading">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="bpmCollect" class="menu" node-key="boxId" :data="bpmCollectDialogList" :props="groupsProps" :current-node-key="''" :highlight-current="true" :expand-on-click-node="false"
							 :filter-node-method="groupFilterNode" @node-click="groupClick">
						<template #default="{node, data}">
							<span class="custom-tree-node">
								<span class="label">{{ node.label }}</span>
								<span class="code">{{ data.code }}</span>
								<span class="do">
									<el-button-group>
										<el-button icon="el-icon-edit" size="small" @click.stop="editBpmCollectBox(data)"></el-button>
										<el-button icon="el-icon-delete" size="small" @click.stop="deleteBpmCollectBox(node, data)"></el-button>
									</el-button-group>
								</span>
							</span>
						</template>

					</el-tree>
				</el-main>
				<el-footer style="height:51px;">
					<el-button type="primary" size="small" icon="el-icon-plus" style="width: 100%;" @click="addBpmCollectBox">添加分类</el-button>
				</el-footer>
			</el-container>
		</el-aside>
		<el-container>
			<el-header style="height: auto;">
				<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
			</el-header>
			<el-header>
				<div class="left-panel">
					<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.keyword" placeholder="收文标题" clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					</div>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :params="{model:'1',isDoc:'2'}" :apiObj="list.apiObj" row-key="recordId" @selection-change="selectionChange" stripe remoteSort remoteFilter>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="流水号" prop="id" width="100" align="center"></el-table-column>
					<el-table-column label="收文标题" prop="flowTitle" width="400" :show-overflow-tooltip="true"></el-table-column>
					<el-table-column label="收藏别名" prop="byName" width="150" align="center"></el-table-column>
					<el-table-column label="收藏分类" prop="boxName" width="150" align="center"></el-table-column>
					<el-table-column label="收藏时间" prop="createTime" width="150" align="center"></el-table-column>
					<el-table-column label="结束时间" prop="endTime" width="150" align="center"></el-table-column>
					<el-table-column label="是否删除" prop="delFlag" width="100" align="center">
						<template #default="scope">
							<el-tag type="danger" v-if="scope.row.delFlag==='1'">删除</el-tag>
							<el-tag type="success">正常</el-tag>
						</template>
					</el-table-column>
					<el-table-column label="状态" prop="status" width="100" align="center">
						<template #default="scope">
							<el-tag v-if="scope.row.status==0" type="success">进行中</el-tag>
							<el-tag v-else type="info">已结束</el-tag>
						</template>
					</el-table-column>
					<el-table-column label="参与人" prop="opUserStr" :show-overflow-tooltip="true"></el-table-column>
					<el-table-column label="操作" fixed="right" align="right" width="160">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small" @click="readBpm(scope.row, scope.$index)">查看</el-button>
								<el-button text type="primary" size="small" @click="deleteBpmCollect(scope.row, scope.$index)">删除</el-button>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<save-collection v-if="dialog.bpmCollect" ref="bpmCollectDialogDialog" @success="getBpmCollectBoxForTree" @closed="dialog.bpmCollect=false"></save-collection>
</template>
<script>
import scSelectFilter from '@/components/scSelectFilter'
import selectUserDialog from "@/views/module/selectUserDialog";
import SaveCollection from "@/views/doc/docrev/approval/saveCollection";
import saveCollection from './saveCollection'
export default {
	name: 'Bpm Collection',
	components: {
		SaveCollection,
		scSelectFilter,
		selectUserDialog,
		saveCollection
	},
	data() {
		return {
			dialog: {
				bpmCollect: false,
			},
			list: {
				apiObj: this.$API.bpm.bpmCollect.getBpmCollectList
			},
			filterData: [
				{
					title: "日期范围",
					key: "date",
					multiple: false,
					options: [
						{
							label: "全部",
							value: ""
						},
						{
							label: "今日",
							value: "1"
						},
						{
							label: "昨日",
							value: "2"
						},
						{
							label: "一周内",
							value: "3"
						},
						{
							label: "一月内",
							value: "4"
						},
						{
							label: "一月前",
							value: "5"
						}
					]
				},
				{
					title: "是否归档",
					key: "sealedStatus",
					multiple: false,
					options: [
						{
							label: "未归档",
							value: "0"
						},
						{
							label: "已归档",
							value: "1"
						}
					]
				}
			],
			selection: [],
			showGrouploading: false,
			groupFilterText: '',
			bpmCollectDialogList: [],
			groupsProps: {
				value: "boxId",
				label: "boxName",
			},
			search: {
				model:'1',
				isDoc:'2',
				sealedStatus:"0",
				boxId:"",
				date:"",
				bpmRunNo:"",
				keyword: "",
			}
		}
	},
	mounted() {
		this.getBpmCollectBoxForTree();
	},
	methods: {
		//表格选择后回调事件
		selectionChange(selection){
			this.selection = selection;
		},
		async getBpmCollectBoxForTree(){
			this.showGrouploading = true;
			var res = await this.$API.bpm.bpmCollect.getBpmCollectBoxForTree.get({model:'1',isDoc:'2'});
			this.showGrouploading = false;
			this.bpmCollectDialogList = res.data;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		filterChange(data) {
			this.search.sealedStatus=data.sealedStatus;
			this.search.date = data.date;
			this.search.boxId = data.boxId;
			this.$refs.table.upData(this.search)
		},
		//树过滤
		groupFilterNode(value, data) {
			if (!value) return true;
			return data.boxName.indexOf(value) !== -1;
		},
		//树点击事件
		groupClick(data) {
			this.search.boxId=data.boxId;
			this.$refs.table.upData(this.search)
		},
		addBpmCollectBox() {
			this.dialog.bpmCollect = true
			this.$nextTick(() => {
				this.$refs.bpmCollectDialogDialog.open()
			})
		},
		editBpmCollectBox(data) {
			this.dialog.bpmCollect = true
			this.$nextTick(() => {
				var editNode = this.$refs.bpmCollect.getNode(data.boxId);
				var editNodeParentId = editNode.level == 1 ? '0' : editNode.parent.data.boxId;
				data.parentId = editNodeParentId
				this.$refs.bpmCollectDialogDialog.open("edit").setData(data)
			})

		},
		deleteBpmCollectBox(node, data) {
			this.$confirm(`确定删除 ${data.boxName} 项吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				var res = await this.$API.bpm.bpmCollect.deleteBpmCollectBox.post({boxId: data.boxId});
				if (res.code == 200) {
					this.$refs.bpmCollect.remove(data.boxId)
					this.$message.success("操作成功")
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
			}).catch(() => {

			})
		},
		batch_del()
		{
			this.$confirm(`确定删除选中的 ${this.selection.length} 收文吗？`, '提示', {
				type: 'warning'
			}).then(async() => {
				const loading = this.$loading();
				var recordIdArr=[];
				this.selection.forEach(item => {
					recordIdArr.push(item.recordId);
				})
				var res = await this.$API.bpm.bpmCollect.deleteBpmCollects.post({recordIds: recordIdArr});
				if (res.code == 200) {
					this.$refs.table.refresh();
					this.$message.success(res.message)
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
				loading.close();
			}).catch(() => {

			})
		},
		async deleteBpmCollect(row, index){
			var res = await this.$API.bpm.bpmCollect.deleteBpmCollect.post({recordId: row.recordId});
			if (res.code == 200) {
				this.$refs.table.refresh();
				this.$message.success("操作成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		readBpm(row, index) {
			this.$nextTick(() => {
				this.$router.replace({
					path: '/doc/handle/read',
					query: {
						runId: row.runId
					}
				})
			})
		},
	}
}
</script>
<style scoped>
.custom-tree-node {
	/*display: flex;*/
	flex: 1;
	align-items: center;
	justify-content: space-between;
	font-size: 14px;
	padding-right: 24px;
	height: 100%;
}

.custom-tree-node .code {
	font-size: 12px;
	color: #999;
}

.custom-tree-node .do {
	margin-left: 10px;
	display: none;
}

.custom-tree-node:hover .code {
	display: none;
}

.custom-tree-node:hover .do {
	margin-left: 10px;
	display: inline-block;
}
</style>
